import time
from commmon.com import BlogDriver
from selenium.webdriver.common.by import By

#测试博客登陆页面
class BlogLogin:
    url = ""
    Driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
    #登录失败为弹窗警告
    def LoginFailTest(self):
        # 前面的输入账号密码没退出，在send会变成拼接，因此需要清空在输入
        self.driver.find_element( By.CSS_SELECTOR, "#username" ).clear()
        self.driver.find_element( By.CSS_SELECTOR, "#password" ).clear()
        self.driver.find_element( By.CSS_SELECTOR, "#username" ).send_keys( "zhangsan" )
        self.driver.find_element( By.CSS_SELECTOR, "#password" ).send_keys( "123" )
        self.driver.find_element( By.CSS_SELECTOR, "#submit" ).click()
        BlogDriver.getScreenShot()
        time.sleep(3)
        # 验证登陆失败（出现弹窗）
        alert = self.driver.switch_to.alert
        #打印弹窗 有弹窗的时候 是不能截图的 所以你这里截图无效 你要么点提前之前截图 要么去掉截图都可以
        #BlogDriver.getScreenShot()
        alert.accept()

    #成功登陆的测试用例
    def LoginSuTest(self):
        self.driver.find_element( By.CSS_SELECTOR, "#username" ).clear()
        self.driver.find_element( By.CSS_SELECTOR, "#password" ).clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(3)
#(会报错可能因为出错就进不去不用验证？)能够找到博客首页的用户昵称，证明登陆成功，否则登陆失败
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        BlogDriver.getScreenShot()








